package com.yanqu.road.dao.impl.activity.huashigang;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangEliteReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 精英怪伤害排行奖励dao
 */
public class HuaShiGangEliteRewardDaoImpl extends TempDataObjectDao<HuaShiGangEliteReward> {

    public static String getHuaShiGangEliteRewardSql = "select * from t_c_huashigang_elite_reward where activityId=?";

    public Map<Integer, List<HuaShiGangEliteReward>> getHuaShiGangEliteRewardMap(long activityId) {
        Map<Integer, List<HuaShiGangEliteReward>> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHuaShiGangEliteRewardSql);
            int i = 1;
            call.setLong(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangEliteReward huaShiGangEliteReward = parse(rs);
                if (!result.containsKey(huaShiGangEliteReward.getY())) {
                    result.put(huaShiGangEliteReward.getY(), new ArrayList<>(3));
                }
                result.get(huaShiGangEliteReward.getY()).add(huaShiGangEliteReward);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateHuaShiGangEliteRewardSql = "update t_c_huashigang_elite_reward set eventId=?,serverId=?,userId=?,reward=?,isGetReward=? where activityId=? and y=? and rank=?";

    @Override
    protected boolean updateData(HuaShiGangEliteReward huaShiGangEliteReward) {
        huaShiGangEliteReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangEliteReward.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangEliteReward.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangEliteReward.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangEliteReward.getReward()));
        para.put(i++, new DbParameter(Types.TINYINT, huaShiGangEliteReward.isGetReward() ? 1 : 0));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangEliteReward.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangEliteReward.getY()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangEliteReward.getRank()));
        if (getDb().executeNoneQuery(updateHuaShiGangEliteRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHuaShiGangEliteRewardSql = "insert into t_c_huashigang_elite_reward(activityId,y,rank,eventId,serverId,userId,reward,isGetReward) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(HuaShiGangEliteReward huaShiGangEliteReward) {
        huaShiGangEliteReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangEliteReward.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangEliteReward.getY()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangEliteReward.getRank()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangEliteReward.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangEliteReward.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangEliteReward.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangEliteReward.getReward()));
        para.put(i++, new DbParameter(Types.TINYINT, huaShiGangEliteReward.isGetReward() ? 1 : 0));
        if (getDb().executeNoneQuery(insertHuaShiGangEliteRewardSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HuaShiGangEliteReward parseData(ResultSet rs) throws SQLException {
        HuaShiGangEliteReward huaShiGangEliteReward = new HuaShiGangEliteReward();
        huaShiGangEliteReward.setActivityId(rs.getLong("activityId"));
        huaShiGangEliteReward.setY(rs.getInt("y"));
        huaShiGangEliteReward.setRank(rs.getInt("rank"));
        huaShiGangEliteReward.setEventId(rs.getInt("eventId"));
        huaShiGangEliteReward.setServerId(rs.getLong("serverId"));
        huaShiGangEliteReward.setUserId(rs.getLong("userId"));
        huaShiGangEliteReward.setReward(rs.getString("reward"));
        huaShiGangEliteReward.setIsGetReward(rs.getInt("isGetReward") == 1);
        return huaShiGangEliteReward;
    }

}
